<?php

class Tuyendung_m extends CI_Model {
   	
	function get_hoso($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'ntd.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'trang_thai'=>	-1,
			'ten'			=>	false,
			'email'		=>	false,			
			'ngay_cap_nhat'	=>	false
		);
		$def = array_merge($def, $data);
		$this->db->select('u.email,ntd.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('nhatuyendung ntd', 'ntd.user_id=u.id');
		$this->db->where('u.trang_thai', 1);
		if($def['trang_thai'] >= 0)
			$this->db->where_in('ntd.trang_thai', $def['trang_thai']);
		if($def['ten'])
			$this->db->where("ntd.ten LIKE '%{$def['ten']}%'", false, false);
		if($def['email'])
			$this->db->where('u.email', $def['email']);
		if($def['ngay_cap_nhat']){
			$this->db->where('ntd.ngay_cap_nhat <', $def['ngay_cap_nhat']);
			return $this->db->get()->row();
		}
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('ntd.ngay_duyet', 'ASC');
		return $this->db->get()->result();
	}
	
	function get_tin_tuyen_dung($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	20,
			'order_by'	=>	'ttd.ngay_dang',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'trang_thai'=>	array(1),
			'tinh_thanh'	=>	false,
			'lhinh'			=>	false,
			// 'dichvu'		   => false
		);
		$def = array_merge($def, $data);
		$this->db->select('ntd.ten,ntd.avatar,ttd.*');
		$this->db->distinct();
		$this->db->from('nhatuyendung ntd');
		$this->db->join('tintuyendung ttd', 'ntd.id=ttd.ntd_id');
		$this->db->where('ntd.trang_thai', 1);
		$this->db->where('ttd.han_hoso >=', date('Y-m-d'));
		$this->db->where_in('ttd.trang_thai', $def['trang_thai']);
		if($def['tinh_thanh'])
			$this->db->where('ttd.tinh_thanh', $def['tinh_thanh']);
		if($def['lhinh'] > 0)
			$this->db->where('ttd.loai_hinh_id', $def['lhinh']);
		// if($def['dichvu'])
			// $this->db->where_in('ttd.loai_hinh_id', $def['lhinh']);
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		$this->db->order_by('vip', 'DESC');
		$this->db->order_by('hot', 'DESC');
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);
		$this->db->order_by('ttd.ngay_duyet', 'ASC');
		return $this->db->get()->result();
	}
	
	function get_tin_da_duyet($id, $ntd_id = false){
		$this->db->select('u.email, ntd.*,lh.ten loai_hinh,ttd.*');
		$this->db->distinct();
		$this->db->from('nhatuyendung ntd');
		$this->db->join('users u', 'u.id=ntd.user_id');
		$this->db->join('tintuyendung ttd', 'ntd.id=ttd.ntd_id');
		$this->db->join('loai_hinh lh', 'lh.id=ttd.loai_hinh_id');
		if($ntd_id)
			$this->db->where('ntd.id', $ntd_id);
		$this->db->where('ttd.id', $id);
		$this->db->where('ttd.han_hoso >=',date('Y-m-d'));
		$this->db->where('ntd.trang_thai', 1);
		$this->db->where('ttd.trang_thai', 1);
		return 
		$this->db->get()->row();
		echo $this->db->last_query();die;
	}
	
	function get_tin_chua_duyet($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'ttd.ngay_dang',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'trang_thai'=>	-1,
			'ngay_dang'	=>	false,
			'tieu_de'	=>	false
		);
		$def = array_merge($def, $data);
		$this->db->select('ntd.ten,ttd.*');
		$this->db->distinct();
		$this->db->from('nhatuyendung ntd');
		$this->db->join('tintuyendung ttd', 'ntd.id=ttd.ntd_id');
		$this->db->where('ntd.trang_thai', 1);
		if($def['trang_thai'] >= 0)
			$this->db->where_in('ttd.trang_thai', $def['trang_thai']);
		if($def['ngay_dang']){
			$this->db->where('ttd.ngay_dang <', $def['ngay_dang']);
			return $this->db->get()->row();
		}
		if($def['tieu_de'])
			$this->db->where("ttd.tieu_de LIKE '%{$def['tieu_de']}%'", false, false);
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('ttd.ngay_duyet', 'ASC');
		return 
		$this->db->get()->result();
		// echo $this->db->last_query();die;
	}
	
	function get_current_profile(){
		$userdata = $this->session->userdata('userdata');
		// echo '<pre>';print_r($userdata);die;
		if($userdata){
			$this->db->select('ntd.*');
			$this->db->from('nhatuyendung ntd');
			$this->db->where('ntd.user_id', $userdata['id']);
			return $this->db->get()->row();
		}
		return false;
	}
	
	function get_object($id, $is_admin = false){		
		$this->db->select('ntd.*');
		$this->db->from('nhatuyendung ntd');
		$this->db->where('ntd.id', $id);
		return 
		$this->db->get()->row();
		echo $this->db->last_query();
	}
	
	function remove($id){
		$this->db->where('id', $id);
		return $this->db->delete('nhatuyendung');
	}
	
	function get_email($id){
		$this->db->select('u.email,ntd.ten');
		$this->db->from('nhatuyendung ntd');
		$this->db->join('users u', 'u.id = ntd.user_id');
		$this->db->where('ntd.id', $id);
		return 
		$this->db->get()->row();
	}
	
	function cap_nhat_trang_thai($data){
		return 
		$this->db->update_batch('nhatuyendung', $data, 'id');
		echo $this->db->last_query();die;
	}
	
	function cap_nhat_trang_thai_tin($data){
		return 
		$this->db->update_batch('tintuyendung', $data, 'id');
		echo $this->db->last_query();die;
	}
	
	function search($fillter, $is_count = false){
		$def = array(
			'limit'				=>	12,
			'order_by'			=>	'cn.ngay_cap_nhat',
			'order'				=>	'DESC',
			'paged'				=>	0,
			'dich_vu'			=>	false,
			'tinh_thanh'		=>	false,
			'chieu_cao_m'		=> 1,
			'chieu_cao_cm_min'=> false,
			'chieu_cao_cm_max'=> false,
			'gioi_tinh'			=>	false,
			'key'					=>	false
		);
		$def = array_merge($def, $fillter);		
		$this->db->select('u.email,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id');
		$this->db->where('u.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		if($def['dich_vu'])
			$this->db->where_in('d.id', $def['dich_vu']);
		if($def['tinh_thanh'])
			$this->db->where('cn.tinh_thanh_id', $def['tinh_thanh']);
		if($def['chieu_cao_cm_min'])
			$this->db->where('cn.chieu_cao_cm >=', $def['chieu_cao_cm_min']);
		if($def['chieu_cao_cm_max'])
			$this->db->where('cn.chieu_cao_cm <=', $def['chieu_cao_cm_max']);
		if($def['gioi_tinh'])
			$this->db->where('cn.gioi_tinh', $def['gioi_tinh']);
		if($def['key']){
			$this->db->or_where('cn.ho_ten LIKE ', '%'.$def['key'].'%');
			$this->db->or_where('cn.hoc_van LIKE ', '%'.$def['key'].'%');
			$this->db->or_where('cn.ngoai_ngu LIKE ', '%'.$def['key'].'%');
			$this->db->or_where('cn.nang_khieu LIKE ', '%'.$def['key'].'%');
		}
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('cn.ngay_duyet', 'ASC');
		return $this->db->get()->result();
	}
	
	function get_loai_hinh(){
		$this->db->where('trang_thai', 1);
		return $this->db->get('loai_hinh')->result();
	}
	
	function update_tin($data){
		if(isset($data['id'])){
			$this->db->where('id', $data['id']);
			$this->db->where('ntd_id', $data['ntd_id']);
			unset($data['id']);
			return 
			$this->db->update('tintuyendung', $data);
			echo $this->db->last_query();die;		
		}
		$this->db->insert('tintuyendung', $data);
		$id = $this->db->insert_id();		
		$code = "TIN".str_pad((int)$id, 4, "0", STR_PAD_LEFT);
		$this->db->where('id', $id);
		return  
		$this->db->update('tintuyendung', array('code'=>$code));		
	}
	
	function get_list_tin($ntd_id){
		$this->db->where('ntd_id', $ntd_id);
		$this->db->order_by('ngay_dang', 'DESC');
		return $this->db->get('tintuyendung')->result();
	}
	
	function get_tin($id, $ntd_id = false){
		if($ntd_id)
			$this->db->where('ttd.ntd_id', $ntd_id);
		$this->db->select('ttd.*,lh.ten loai_hinh');
		$this->db->join('loai_hinh lh', 'ttd.loai_hinh_id = lh.id');
		$this->db->where('ttd.id', $id);
		return $this->db->get('tintuyendung ttd')->row();
	}
	
	function xoa_tin($id, $ntd_id){
		$this->db->where('ntd_id', $ntd_id);
		$this->db->where('id', $id);
		return $this->db->delete('tintuyendung');
	}
	
	function get_related($data, $id){
		if(!$data){
			$this->db->where('id <', $id);			
			return $this->db->get('tintuyendung')->result();
		}		
		if(count($data) == 1 && empty($data[0]))
			return array();
		// array_push($data, 0);
		// array_unshift($data, -1);
		// echo '<pre>';print_r($data);die;
		if(count($data) == 1)
			$str = implode("%' OR dich_vu LIKE '%", $data);
		else
			$str = implode(",%' OR dich_vu LIKE '%,", $data);
		$str = "dich_vu LIKE '%".$str;
		$str .= "%'";
		$sql = "SELECT * FROM tintuyendung WHERE trang_thai IN (1) AND id <> $id AND($str) AND han_hoso > now() LIMIT 5";
		// echo $sql;die;
		return $this->db->query($sql)->result();
		// echo $this->db->last_query();die;
	}
	
	function apply($data){
		$this->db->where('ntd_id', $data['ntd_id']);
		$this->db->where('cn_id', $data['cn_id']);
		$this->db->where('ttd_id', $data['ttd_id']);
		$this->db->delete('nophoso');
		return $this->db->insert('nophoso', $data);
	}
	
	function is_apply($cn_id, $ttd_id, $ntd_id){
		$this->db->where('ntd_id', $ntd_id);
		$this->db->where('cn_id', $cn_id);
		$this->db->where('ttd_id', $ttd_id);
		return $this->db->get('nophoso')->row();
	}
	
	function get_hoso_datraphi($ntd_id){
		$this->db->select('cn.id,cn.ho_ten,cn.avatar');
		$this->db->from('hoso_datraphi hs');
		$this->db->join('canhan cn', 'cn.id = hs.cn_id');
		$this->db->where('hs.ntd_id', $ntd_id);
		$this->db->where('cn.trang_thai', 1);
		$this->db->order_by('hs.ngay_xem', 'DESC');
		return $this->db->get()->result();
	}
	
	function get_hoso_danop($ntd_id){
		$this->db->select('cn.id,cn.ho_ten,cn.avatar,ttd.id ttd_id,ttd.tieu_de');
		$this->db->from('nophoso nhs');
		$this->db->join('tintuyendung ttd', 'nhs.ttd_id = ttd.id');
		$this->db->join('canhan cn', 'cn.id = nhs.cn_id');
		$this->db->where('nhs.ntd_id', $ntd_id);
		$this->db->where('ttd.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		$this->db->order_by('ttd.ngay_dang', 'DESC');
		$this->db->order_by('nhs.ngay_nop', 'ASC');
		return $this->db->get()->result();
	}
	
	/*
	*	@hien thi phone & cat-xe neu du poin de xem
	*	@ntd_id	:	nha tuyen dung id
	*	@point	:	so diem phai tra cho 1 profile
	*/
	function get_phone_catse($user_id, $cn_id, $point, $content, $type){
		$sql = "CALL `get_phone_catse`('$user_id', '$cn_id', '$point', '$content', '$type')";
		// echo $sql;die;
		return $this->db->query($sql)->row();
	}
	
	/*
	*	Kiem tra ho so da duoc xem trong vong 24h hay chua
	*/
	function is_viewed($cn_id){
		$user	=	$this->session->userdata('userdata');
		if(!$user || !$user['ntd_id'])
			return false;		
		$this->db->select('count(*) total');
		$this->db->from('hoso_datraphi');
		$this->db->where('ntd_id', $user['ntd_id']);
		$this->db->where('cn_id', $cn_id);
		$this->db->where('ngay_xem between DATE_SUB(now(), INTERVAL 24 HOUR) AND NOW()');
		$row =	$this->db->get()->row();
		// echo $this->db->last_query();die;
		return $row->total;
	}
	
	function get_point(){
		$user	=	$this->session->userdata('userdata');
		if($user['id']){
			$id	=	$user['id'];
			return $this->db->where('user_id', $id)->get('user_point')->row();
		}			
		return false;
	}
	
	function get_history_payment($id){
		$this->db->select('*');
		$this->db->where('user_id', $id);
		$this->db->order_by('ngay', 'desc');
		return 
		$this->db->get('thanh_toan')->result();
		// echo $this->db->last_query();die;
	}
}
